package com.xhwl.logistics.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.vehicle.VehcPayRecord;
import com.xhwl.logistics.bo.VehiclePayRecordBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.bo.cent.VehicleSystemInfoBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehiclePayRecordBO;
import com.xhwl.logistics.bo.input.QueryVehiclePayRecordForCountBO;
import com.xhwl.logistics.component.SignForCoSon;
import com.xhwl.logistics.converter.ToPage;
import com.xhwl.logistics.converter.ToVehiclePayRecordBO;
import com.xhwl.logistics.dao.VehcPayRecordDao;
import com.xhwl.logistics.dao.mapper.IVehiclePayRecordMapper;
import com.xhwl.logistics.dto.push.CarPayDataCoSonDTO;
import com.xhwl.logistics.entity.VehiclePayRecord;
import com.xhwl.logistics.po.VehiclePayRecordPO;
import com.xhwl.logistics.service.IVehiclePayRecordService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

//@Service
public class VehiclePayRecordServiceImpl extends ServiceImpl<IVehiclePayRecordMapper, VehiclePayRecord> implements IVehiclePayRecordService {

    @Autowired
    private IVehiclePayRecordMapper vehiclePayRecordMapper;

    @Autowired
    private VehcPayRecordDao vehcPayRecordDao;

    @Autowired
    private ToPage toPage;

    @Autowired
    private ToVehiclePayRecordBO toVehiclePayRecordBO;
    @Autowired
    private QueryDeviceService queryDeviceService;
    @Autowired
    private SignForCoSon signForCoSon;

    @Override
    public Page<VehiclePayRecordBO> pagePayRecord(QueryVehiclePayRecordBO queryVehiclePayRecordBO, PageBO pageBO) {
        IPage<VehiclePayRecordPO> payRecordPOPage = vehiclePayRecordMapper.pagePayRecord(pageBO.toPage(), queryVehiclePayRecordBO);

        return toPage.toPage(payRecordPOPage, toVehiclePayRecordBO.toVehiclePayRecordBOList(payRecordPOPage.getRecords()));
    }

    @Override
    public Integer payRecordForCount(QueryVehiclePayRecordForCountBO queryVehiclePayRecordForCountBO) {
        return vehiclePayRecordMapper.payRecordForCount(queryVehiclePayRecordForCountBO);
    }

    @Override
    public VehicleParkingInfoBO carPay(CarPayDataCoSonDTO carPayDataCoSonDTO) {
        VehcPayRecord vehcPayRecord = new VehcPayRecord();
        vehcPayRecord.setParkId(carPayDataCoSonDTO.getKey());
        vehcPayRecord.setPlateNo(carPayDataCoSonDTO.getCarNo());
        vehcPayRecord.setOrderNo(carPayDataCoSonDTO.getOrderNo());
        vehcPayRecord.setPaymentOrderNo(carPayDataCoSonDTO.getPayOrderNo());
        Date edate = DateUtil.parse(carPayDataCoSonDTO.getEnterTime());
        Date pdate = DateUtil.parse(carPayDataCoSonDTO.getPayTime());
        vehcPayRecord.setEntryTime(edate);
        vehcPayRecord.setPayTime(pdate);
        if(null == carPayDataCoSonDTO.getPayMoney()){
            throw new BusinessException("缺少必填项:PayMoney");
        }else if(carPayDataCoSonDTO.getPayMoney().isEmpty()) {
            throw new BusinessException("PayMoney不能为空");
        }else {
            vehcPayRecord.setTotalMoney((new BigDecimal(carPayDataCoSonDTO.getPayMoney()).multiply(new BigDecimal("100"))).intValue());
        }

        if(null == carPayDataCoSonDTO.getPayedMoney() || carPayDataCoSonDTO.getPayedMoney().isEmpty()){
            if((null == carPayDataCoSonDTO.getCouponMoney()) || carPayDataCoSonDTO.getCouponMoney().isEmpty()){
                vehcPayRecord.setPaidMoney(vehcPayRecord.getTotalMoney());
                vehcPayRecord.setFreeMoney(0);
            }else{
                vehcPayRecord.setFreeMoney((new BigDecimal(carPayDataCoSonDTO.getCouponMoney()).multiply(new BigDecimal("100"))).intValue());
                vehcPayRecord.setPaidMoney(vehcPayRecord.getTotalMoney()-vehcPayRecord.getFreeMoney());
            }
        }else {
            vehcPayRecord.setPaidMoney((new BigDecimal(carPayDataCoSonDTO.getPayedMoney()).multiply(new BigDecimal("100"))).intValue());
            if ((null == carPayDataCoSonDTO.getCouponMoney()) || carPayDataCoSonDTO.getCouponMoney().isEmpty()) {
                vehcPayRecord.setFreeMoney(vehcPayRecord.getTotalMoney() - vehcPayRecord.getPaidMoney());
            } else {
                Integer freeMoney = (new BigDecimal(carPayDataCoSonDTO.getCouponMoney()).multiply(new BigDecimal("100"))).intValue();
                if (freeMoney != vehcPayRecord.getTotalMoney() - vehcPayRecord.getPaidMoney()) {
                    throw new BusinessException("金额有误");
                } else {
                    vehcPayRecord.setFreeMoney(freeMoney);
                }
            }
        }
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromParkId(vehcPayRecord.getParkId());
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();
        VehicleSystemInfoBO vehicleSystemInfoBO = vehicleIntegratedConfigBO.getVehicleSystemInfoBO();
        vehcPayRecord.setProjectId(vehicleSystemInfoBO.getProjectId());
        vehcPayRecordDao.insert(vehcPayRecord);

        return vehicleParkingInfoBO;
    }

}
